package com.ruan.service;

import com.ruan.dao.AddressMapper;
import com.ruan.dao.UserAddressMapper;
import com.ruan.entity.Address;
import com.ruan.entity.Result;
import com.ruan.entity.UserAddress;
import com.ruan.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName:UserAddressService
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/7/3 10:23
 **/
@Service
public class UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private AddressMapper addressMapper;

    public Result getAddressListById(Integer id) {
        List<AddressVo> userAddressList = userAddressMapper.getAddressListById(id);
        for (AddressVo i:userAddressList){
            i.setAddress(addressMapper.getAddressById(i.getAddressId()));
        }
        return new Result(200,userAddressList);
    }

    @Transactional
    public Result deleteAddress(Integer id) {
        Integer addressId = userAddressMapper.getAddressIdById(id);
        int row1 = userAddressMapper.deleteAddress(id);
        int row2 = addressMapper.deleteAddress(addressId);
        if(row1 != row2){
            return new Result(100,"地址删除失败");
        }else {
            return new Result(200,"地址删除成功");
        }
    }

    public Result editAddress(AddressVo addressVo) {
        Integer addressId = userAddressMapper.getAddressIdById(addressVo.getId());
        addressVo.setAddressId(addressId);
        int row = addressMapper.editAddress(addressVo);
        if(row != 1){
            return new Result(100,"地址修改失败");
        }else {
            return new Result(200,"地址修改成功");
        }
    }
}
